<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>模块管理</title>

	<link rel="stylesheet" type="text/css" href="/admin/css/bootstrap.min.css" />
	<link rel="stylesheet" type="text/css" href="/admin/css/style.min.css" />
	<link rel="stylesheet" type="text/css" href="/admin/css/font-awesome.css" />
	<link rel="stylesheet" href="/admin/layui/css/layui.css">

	<script src="/admin/js/jquery-3.3.1.min.js"></script>
	<script src="/admin/layui/layui.js" charset="utf-8"></script>
	<script src="/admin/js/base.js" charset="utf-8"></script>

	<!-- 树 -->
	<link rel="stylesheet" href="/lib/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="/lib/zTree/js/jquery.ztree.core-3.5.js"></script>
	<script type="text/javascript" src="/lib/zTree/js/jquery.ztree.excheck-3.5.js"></script>
	<script type="text/javascript" src="/lib/zTree/js/jquery.ztree.exedit-3.5.js"></script>
	<script src="/admin/js/api.js" charset="utf-8"></script>
	<script src="/admin/js/common.js" charset="utf-8"></script>
</head>

<body>
<div class="rigthContent">
	<div class="search-top">
		<div class="tool_bar">
			<a href="javascript:;" id="expandAllBtn" class="btn btn-default btn-sm">
				<span class="icon icon-double-angle-down handle_ioc"></span>
				展开
			</a>
			<a href="javascript:;"  id="resetBtn" class="btn btn-default btn-sm">
				<span class="icon icon-double-angle-up handle_ioc"></span>
				收起
			</a>
		</div>
	</div>
	<div class="pageContent">
		<div class="manageeditdiv" style="padding: 0 20px 20px 20px;">
			<span id="role_name" style="text-align: center;display: block;font-size: 16px;"></span>
			<br/>
			<ul id="treeDemo" class="ztree" style="-moz-user-select: none;">
			</ul>
		</div>
	</div>
</div>

<SCRIPT type="text/javascript">
	var roleId = getParam("id");
	var name = getParam("name");
    var setting = {
        async: {
            enable: true,
            url:Url.creatUrl(Api.admin.base.role.getAuthorTree, {roleId: roleId}, null),
            autoParam:["id"],
            dataFilter: function(treeId, parentNode, childNodes){
                if (!childNodes) return null;
                return childNodes;
            }
        },
        check: {
            enable: true,
            chkboxType: { "Y" : "ps", "N" : "s" },
            autoCheckTrigger: true
        },
        callback: {
            beforeCheck: function(treeId, treeNode) {
                if (treeNode.checked) {
                    return uncheckedNode(treeNode.id);
                } else {
                    return checkedNode(treeNode.id);
                }
            },
            beforeAsync: beforeAsync,
            onAsyncSuccess: onAsyncSuccess,
            onAsyncError: onAsyncError
        },
        view: {
            addDiyDom: function(treeId, treeNode) {

                $("#treeDemo li").hover(function(){
                    $(this).addClass("hover")
                },function(){
                    $(this).removeClass("hover")
                })

            }
        }
    };

    function uncheckedNode(treeId) {
        _loading(); //加载等待
        var data = This.jqueryAjax(Api.admin.base.role.cancelAuthorize, {moduleId: treeId, roleId: roleId});
        _loadingClose(); //关闭加载等待
        if (data.status == 1) {
            layer.msg(data.message, {icon:1});
            return true;
        } else {
            layer.msg(data.message, {icon:2});
            return false;
        }
    }

    function checkedNode(treeId) {
        _loading(); //加载等待
        var data = This.jqueryAjax(Api.admin.base.role.addAuthorize, {moduleId: treeId, roleId: roleId});
        _loadingClose(); //关闭加载等待
        if (data.status == 1) {
            layer.msg(data.message, {icon:1});
            return true;
        } else {
            layer.msg(data.message, {icon:2});
            return false;
        }

    }

    function beforeAsync() {
        curAsyncCount++;
    }

    function onAsyncSuccess(event, treeId, treeNode, msg) {
        curAsyncCount--;
        if (curStatus == "expand") {
            expandNodes(treeNode.children);
        } else if (curStatus == "async") {
            asyncNodes(treeNode.children);
        }

        if (curAsyncCount <= 0) {
            if (curStatus != "init" && curStatus != "") {
                $("#demoMsg").text((curStatus == "expand") ? demoMsg.expandAllOver : demoMsg.asyncAllOver);
                asyncForAll = true;
            }
            curStatus = "";
        }
    }

    function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
        curAsyncCount--;

        if (curAsyncCount <= 0) {
            curStatus = "";
            if (treeNode!=null) asyncForAll = true;
        }
    }

    var curStatus = "init", curAsyncCount = 0, asyncForAll = false,
        goAsync = false;
    function expandAll() {
        if (!check()) {
            return;
        }
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        if (asyncForAll) {
            zTree.expandAll(true);
        } else {
            expandNodes(zTree.getNodes());
            if (!goAsync) {
                curStatus = "";
            }
        }
    }
    function expandNodes(nodes) {
        if (!nodes) return;
        curStatus = "expand";
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        for (var i=0, l=nodes.length; i<l; i++) {
            zTree.expandNode(nodes[i], true, false, false);
            if (nodes[i].isParent && nodes[i].zAsync) {
                expandNodes(nodes[i].children);
            } else {
                goAsync = true;
            }
        }
    }

    function reset() {
        if (!check()) {
            return;
        }
        asyncForAll = false;
        goAsync = false;
        $("#demoMsg").text("");
        $.fn.zTree.init($("#treeDemo"), setting);
    }
    function check() {
        if (curAsyncCount > 0) {
            $("#demoMsg").text(demoMsg.async);
            return false;
        }
        return true;
    }

    $(document).ready(function(){
        $.fn.zTree.init($("#treeDemo"), setting);
        // 加载layui模块
        layuiReady (function() {
            _loadingClose();
		});

        $("#expandAllBtn").bind("click", expandAll);
        $("#resetBtn").bind("click", reset);

        $("#role_name").text('您正在为【' + name + '】授权');
    });
</script>
</body>

</html>